package com.yunhe.fountain.scoket;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.util.StringUtil;
import com.yunhe.fountain.constant.FountainConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * @ClassName CardFlow
 * @Description TODO
 * @Author HeTao
 * @Date 2021/7/7 19:32
 * @Version 1.0
 **/
public class CardFlow {

    private static final Logger log = LoggerFactory.getLogger(CardFlow.class);
    static ClientLaunch cl= new ClientLaunch();

    static  Socket socket;

    static SocketWriter socketWriter= new SocketWriter();

    static InputStream socketReader;

    /**
     * 判断是否建立连接成功
     * @param host
     * @param port
     * @return
     * @throws IOException
     */
    public static String establishConnection(String host, int port) throws IOException {
        try {
            socket = new Socket(host, port);
            if(socket.isConnected()){
                return "connection_success";
            }else {
                return "connection_fail";
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "connection_fail";
        }finally {
            socket.close();
        }
    }

    /**
     * 下发指令
     * @param host
     * @param port
     * @param newCommand
     * @return  connection_fail 建立连接失败  carry_on_success 继续成功   carry_on_fail继续失败 stop_fail 停止失败    stop_success 停止成功
     * start_program_fail 开启节目失败    start_program_success 开启节目成功 issue_order_fail 下发指令失败 issue_order_success 下发指令成功
     * @throws IOException
     * @throws ArgumentErrorException
     */
    public static String issueAnOrder(String host, int port,String newCommand) throws ArgumentErrorException, IOException {
        try {
            socket = new Socket(host, port);
        } catch (IOException e) {
            e.printStackTrace();
            return "connection_fail";
        }
        if(!socket.isConnected()){
            throw new ArgumentErrorException("connection_fail");
        }
        if(FountainConstant.COMMAND_CONSTANT.carryOn.value().equals(newCommand) || FountainConstant.COMMAND_CONSTANT.Stop.value().equals(newCommand)){
            String client = cl.Client(socket, socketWriter, socketReader, newCommand);
            log.info("给喷泉下发"+FountainConstant.COMMAND_CONSTANT.getTitle(newCommand)+"指令成功");
            Assert.isTrue(socket.isConnected(),"与喷泉设备连接成功");
            socket.close();
            try {
                socket = new Socket(host, port);
            } catch (IOException e) {
                e.printStackTrace();
                return "connection_fail";
            }
            if(!socket.isConnected()){
                throw new ArgumentErrorException("connection_fail");
            }
            String message = cl.Client(socket, socketWriter, socketReader, FountainConstant.COMMAND_CONSTANT.selectStatus.value());
            socket.close();
            if(FountainConstant.COMMAND_CONSTANT.carryOn.value().equals(newCommand) && !StringUtil.isEmpty(message)){
                if("030303".equals(message)){
                    return "carry_on_success";
                }else if("030300".equals(message)){
                    return "carry_on_fail";
                }
            }else if(FountainConstant.COMMAND_CONSTANT.Stop.value().equals(newCommand) && !StringUtil.isEmpty(message)){
                if("030303".equals(message)){
                    return "stop_fail";
                }else if("030300".equals(message)){
                    return "stop_success";
                }
            }else if(StringUtil.isEmpty(message)){
                return "issue_order_success";
            }
        }else{
              try {
                  String message1 = cl.Client(socket, socketWriter, socketReader, newCommand);
                  log.info("给喷泉下发"+FountainConstant.COMMAND_CONSTANT.getTitle(newCommand)+"指令成功");
                  Assert.isTrue(socket.isConnected(),"与喷泉设备连接成功");
                  socket = new Socket(host, port);
                  if(!socket.isConnected()){
                      return "connection_fail";
                  }
                  socket.close();
                  return "start_program_success";
              }catch (Exception e){
                  log.error("给喷泉下发"+FountainConstant.COMMAND_CONSTANT.getTitle(newCommand)+"指令失败");
                  return "start_program_fail";
              }

//            String client = cl.Client(socket, socketWriter, socketReader, FountainConstant.COMMAND_CONSTANT.Stop.value());
//            log.info("给喷泉下发"+FountainConstant.COMMAND_CONSTANT.getTitle(FountainConstant.COMMAND_CONSTANT.Stop.value())+"指令成功");
//            Assert.isTrue(socket.isConnected(),"与喷泉设备连接成功");
//            socket = new Socket(host, port);
//            if(!socket.isConnected()){
//                throw new ArgumentErrorException("connection_fail");
//            }
//            String message = cl.Client(socket, socketWriter, socketReader, FountainConstant.COMMAND_CONSTANT.selectStatus.value());
//            if(!StringUtil.isEmpty(message) && "030303".equals(message)){
//                socket.shutdownOutput();
//                return "start_program_fail";
//            }
//            String message1 = cl.Client(socket, socketWriter, socketReader, newCommand);
//            log.info("给喷泉下发"+FountainConstant.COMMAND_CONSTANT.getTitle(newCommand)+"指令成功");
//            Assert.isTrue(socket.isConnected(),"与喷泉设备连接成功");
//            socket = new Socket(host, port);
//            if(!socket.isConnected()){
//                throw new ArgumentErrorException("connection_fail");
//            }
//            String message2 = cl.Client(socket, socketWriter, socketReader, FountainConstant.COMMAND_CONSTANT.selectStatus.value());
//            if(!StringUtil.isEmpty(message2) && "030303".equals(message2)){
//                socket.shutdownOutput();
//                return "start_program_success";
//            }else if(!StringUtil.isEmpty(message2) && "030300".equals(message2)){
//                socket.shutdownOutput();
//                return "start_program_fail";
//            }
        }
        return "issue_order_fail";
    }

    public static void main(String[] args) throws Exception {
          System.out.println(issueAnOrder("47.108.95.221", 6126, "0200"));
          //System.out.println(establishConnection("47.108.95.221", 6126));
//        socket = new Socket("47.108.95.221", 6126);
//        boolean connected = socket.isConnected();
//        if(!connected){
//            throw new Exception("未连接");
//        }
////        String client = cl.Client(socket, socketWriter, socketReader, "0000");
////        System.out.println("通信成功："+client);
//        //Assert.isTrue(socket.isConnected(),"连接成功");
//        socket = new Socket("47.108.95.221", 6126);
//        String client1 = cl.Client(socket, socketWriter, socketReader, "0300");
//        System.out.println("通信成功："+client1);
//        socket.close();
    }
}
